define(function (require) {
    require('bootstrapValidator');
    require('messager');
    require('table');
    require('daterangepicker');
    require('datetimepicker');
    require('moment');
    require('iCheck');

    $.module("BASE.OrgVersion", function () {

        return {
            init: function () {
                this.loadData();
                this.loadEvent();
            },
            clearDatas:function () {
                var that = this;

                $("#_id").val(0);
                $("#Version").val('');

                $("input[name='Active'][value='0']").iCheck('check');

                $('#fm').bootstrapValidator('resetForm', true);
            },
            loadData: function (pageNumber, pageSize) {
                var that = this;

                let s_Id = $("#s_Id").val();
                let s_Version = $("#s_Version").val();
                let s_Active = $("#s_Active").val();

                SYS.Core.ajax({
                    url: "base/orgVersion/list",
                    common: {
                        page: pageNumber ? pageNumber : 1,
                        size: pageSize ? pageSize : 20
                    },
                    data: {
                        Id: s_Id || 0,
                        Version: s_Version,
                        Active: s_Active || -1
                    },
                    success: function (data) {
                        var obj = {
                            'pageNumber': data.data.extra.current_page,
                            'pageSize': data.data.extra.page_size,
                            'totalRows': data.data.extra.all_count,
                            'data': data.data.list
                        };

                        $("#myTable").bootstrapTable('destroy');
                        $('#myTable').bootstrapTable({
                            striped: true,
                            singleSelect: false,
                            showColumns: true,
                            showToggle: true,
                            pagination: true,
                            pageNumber: obj.pageNumber,
                            pageSize: obj.pageSize,
                            totalRows: obj.totalRows,
                            pageList: [20, 50, 100, 200],
                            sidePagination: 'server',
                            clickToSelect: true,
                            idField: 'id',
                            data: obj.data,
                            columns: [
                                // {field: 'checked', checkbox: true} ,
                                {field: 'Id', title: '组织机构版本ID', align: 'center'},
                                {field: 'Version', title: '组织机构版本号', align: 'center'},
                                {field: 'Active', title: '是否启用', align: 'center',
                                    formatter: function (value,row) {
                                        return value == 1 ? '<label class="text-success">启用</label>' : '<label class="text-danger">不启用</label>';
                                    }
                                },
                                {
                                    field: 'do', title: '操作', align: 'center',
                                    formatter: function (value, row, index) {
                                        var html = '';
                                        html += '<a href="javascript:void(0)" onclick="BASE.OrgVersion.toEdit(' + row.Id + ')" class="text-do-edit"><i class="fa fa-pencil"></i> 编辑</a>';
                                        html += '<a href="javascript:void(0)" onclick="BASE.OrgVersion.toRemove(' + row.Id + ')" class="text-do-remove"><i class="fa fa-trash"></i> 删除</a>';
                                        if (row.Active == 0) {
                                            html += '<a href="javascript:void(0)" onclick="BASE.OrgVersion.toOnLine(' + row.Id + ')" class="text-do-add"><i class="fa fa-check"></i> 启用</a> ';
                                        } else if (row.Active == 1) {
                                            html += '<a href="javascript:void(0)" onclick="BASE.OrgVersion.toOffLine(' + row.Id + ')" class="text-do-remove"><i class="fa fa-close""></i> 不启用</a>';
                                        }
                                        return html;
                                        return html;
                                    }
                                }
                            ],
                            onPageChange: function(number, size){
                                that.loadData(number, size);
                            },
                            onRefreshTable: function () {
                                that.loadData();
                            },
                            onClickCell: function (field, value, row, $element) {
                            }
                        });
                    }
                });
            },
            loadEvent: function () {
                var that = this;

                $(".i-checks").iCheck({checkboxClass:"icheckbox_square-orange", radioClass:"iradio_square-orange"});
                SYS.Tool.initDaterangSearch('s_create_time', 's_create_time_begin', 's_create_time_end');

                // 定义编辑框form
                var $form = $("#fm");

                // 查询
                $("#search-toolbar").on('click', "#btn_search", function () {
                    that.loadData();
                }).on("keydown ", "input", function (e) {
                    if (e.keyCode == 13) {
                        that.loadData();
                    }
                });

                // 新增
                $("#btn_add").on('click', function () {
                    that.clearDatas();

                    $('#myModal').modal('show');
                });

                $('#fm').bootstrapValidator({
                    feedbackIcons: {
                        valid: 'glyphicon glyphicon-ok',
                        invalid: 'glyphicon glyphicon-remove',
                        validating: 'glyphicon glyphicon-refresh'
                    },
                    fields: {
                        Version: {
                            validators: {
                                notEmpty: {
                                    message: '此项必填'
                                }
                            }
                        }
                    }
                }).on('success.form.bv', function (e) { //使用此方法可以ajax
                    e.preventDefault();

                    //保存角色
                    that.save();
                });

                //清空验证信息(必须)
                $('#myModal').on('hide.bs.modal', function() {
                    that.clearDatas();
                });
            },
            toEdit: function (id) {
                var that = this;
                that.clearDatas();

                SYS.Core.ajax({
                    url: 'base/orgVersion/detail',
                    common: {},
                    data: {
                        Id: id ? id : 0
                    },
                    success: function (data) {
                        if (data.code == 1) {
                            let obj = data.data;

                            let id = obj.Id;
                            let version = obj.Version;
                            let active = obj.Active;

                            $("input[name='_id']").val(id);
                            $("input[name='Version']").val(version);
                            $("input[name='Active'][value='" + (active != null ? active : 0) + "']").iCheck('check');

                            $('#myModal').modal('show');
                        }
                    }
                });
            },
            toRemove: function (id) {
                var that = this;

                $.messager.confirm("确定要删除吗？", function() {
                    SYS.Core.ajax({
                        url: "base/orgVersion/delete",
                        common: {},
                        data: {
                            ids: [id]
                        },
                        success: function (data) {
                            if(data.code == 1){
                                $.messager.popup(data.msg, "success");
                                that.loadData();
                            }else{
                                $.messager.popup(data.msg,'error');
                            }
                        }
                    })
                });
            },
            save: function (){
                var that = this;

                let id = $("#fm input[name='_id']").val();
                let Version = $("#fm input[name='Version']").val();
                let Active = $("input[name='Active']:checked").val()

                SYS.Core.ajax({
                    url: 'base/orgVersion/save',
                    common: {},
                    data: {
                        Id: id || 0,
                        Version: Version,
                        Active: Active
                    },
                    success: function (data) {
                        if(data.code == 1){
                            $('#myModal').modal('hide');

                            $.messager.popup('保存成功','success');
                            that.loadData();
                        }else{
                            $('#fm').bootstrapValidator('disableSubmitButtons', false);
                            $.messager.popup('保存失败','error');
                        }
                    }
                })
            },
            toOffLine: function (id) {
                var that = this;

                $.messager.confirm("确定要不启用吗？", function () {
                    SYS.Core.ajax({
                        url: "base/orgVersion/offline",
                        common: {},
                        data: {
                            Id: id
                        },
                        success: function (data) {
                            if (data.code == 1) {
                                $.messager.popup('启用成功', "success");

                                that.loadData();
                            } else {
                                $.messager.popup('启用失败', 'error');
                            }
                        }
                    })
                });
            },
            toOnLine: function (id) {
                var that = this;
                $.messager.confirm("确定要启用吗？", function () {
                    SYS.Core.ajax({
                        url: "base/orgVersion/online",
                        common: {},
                        data: {
                            Id: id
                        },
                        success: function (data) {
                            if (data.code == 1) {
                                $.messager.popup('不启用成功', "success");

                                that.loadData();
                            } else {
                                $.messager.popup('不启用失败', 'error');
                            }
                        }
                    })
                });
            },
        }
    });

    BASE.OrgVersion.init();
});